﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.IndexMarkServiceReference;
using System.Collections.ObjectModel;
using AdminModule.CommonClasses;
using AdminModule.Filter;
using System.Windows.Data;

namespace AdminModule.UserControls.Common
{
    public partial class LogFilter : UserControl
    {
        public delegate void AddedHandler(int count, int rowHeight);
        public event AddedHandler Added;

        public delegate void DeletedHandler(int count, int rowHeight);
        public event DeletedHandler Deleted;

        private readonly int rowHeight = 28;

        private ObservableCollection<FilterItem> filterItems = new ObservableCollection<FilterItem>();
        public ObservableCollection<FilterItem> FilterItems
        {
            get
            {
                return filterItems;
            }
            set
            {
                filterItems = value;
            }
        }

        System.Collections.IEnumerable FilterAttributes = null;

        public LogFilter()
        {
            InitializeComponent();

            Binding binding = new Binding("FilterItems");
            binding.Source = this;
            binding.Mode = BindingMode.TwoWay;

            listBox.SetBinding(ListBox.ItemsSourceProperty, binding);
        }

        public void InitFilter(System.Collections.IEnumerable FilterAttributes)
        {
            this.FilterAttributes = FilterAttributes;
        }

        public double GetMinHeight
        {
            get
            {
                return listBox.MinHeight;
            }
        }

        public ObservableCollection<FilterItem> GetFilters()
        {
            return FilterItems;
        }

        private void addCondition_Click(object sender, RoutedEventArgs e)
        {
            FilterItems.Add(new FilterItem(FilterAttributes));
            CheckItemsCount();
            RaiseAdded();
        }

        private void removeCondition_Click(object sender, RoutedEventArgs e)
        {
            FilterItems.Remove((sender as Button).Tag as FilterItem);
            CheckItemsCount();
            RaiseDeleted();
        }

        private void addFilter_Click(object sender, RoutedEventArgs e)
        {
            addCondition_Click(sender, e);
        }

        private void CheckItemsCount()
        {
            if (FilterItems.Count == 0)
            {
                addFilter.Visibility = System.Windows.Visibility.Visible;
                listBox.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                addFilter.Visibility = System.Windows.Visibility.Collapsed;
                listBox.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            FilterItem fi = txtBox.Tag as FilterItem;
            fi.Condition = txtBox.Text;
        }

        private void AttributeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            FilterItem fi = cb.Tag as FilterItem;
            fi.SelectedAttibute = cb.SelectedItem.GetType().GetProperty("Attribute").GetValue(cb.SelectedItem, null);

            StackPanel stackPanel = cb.Parent as StackPanel;
            ComboBox comboBox = stackPanel.FindName("ValueComboBox") as ComboBox;
            TextBox textBox = stackPanel.FindName("ValueTextBox") as TextBox;

            if (cb.SelectedItem.GetType().GetProperty("AllowedValues").GetValue(cb.SelectedItem, null) == null)
            {
                comboBox.Visibility = System.Windows.Visibility.Collapsed;
                textBox.Visibility = System.Windows.Visibility.Visible;
                fi.Condition = null;
            }
            else
            {
                textBox.Visibility = System.Windows.Visibility.Collapsed;
                comboBox.ItemsSource = cb.SelectedItem.GetType().GetProperty("AllowedValues").GetValue(cb.SelectedItem, null) as System.Collections.IEnumerable;
                comboBox.Visibility = System.Windows.Visibility.Visible;
                fi.Condition = null;
            }

            fi.IsString = (bool)cb.SelectedItem.GetType().GetProperty("IsString").GetValue(cb.SelectedItem, null);
        }

        private void ConditionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            FilterItem fi = cb.Tag as FilterItem;
            if (cb.SelectedItem == null) return;
            fi.ConditionType = (cb.SelectedItem as Condition).ConditionType;
        }

        private void ValueComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            FilterItem fi = cb.Tag as FilterItem;
            fi.Condition = (cb.SelectedItem as FilterAllowedValues).Value;
        }

        private void RaiseAdded()
        {
            if (Added != null)
            {
                Added(FilterItems.Count, rowHeight);
            }
        }

        private void RaiseDeleted()
        {
            if (Deleted != null)
            {
                Deleted(FilterItems.Count, rowHeight);
            }
        }
    }
}
